/**
 * 报表设计公司(2011)
 */
package com.reportgear.designer.pane.style;

import java.awt.AWTEvent;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Cursor;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.FocusEvent;
import java.awt.event.FocusListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.ArrayList;
import java.util.List;

import javax.swing.AbstractButton;
import javax.swing.BorderFactory;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.Icon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPopupMenu;
import javax.swing.JSeparator;
import javax.swing.UIManager;
import javax.swing.border.Border;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import com.reportgear.core.swing.pane.base.JBasePane;
import com.reportgear.core.util.ImageUtils;
import com.reportgear.designer.ui.base.ToolBarButton;
import com.reportgear.report.context.ReportContext;
import com.reportgear.report.model.cell.style.BorderStyle;
import com.reportgear.report.view.JWorksheet;
import com.reportgear.swing.util.GuiUtils;

/**
 * 边框样式选择面板
 * 
 * @version 1.0 2011-3-24
 * @author <a herf="lexloo@gmail.com">lexloo</a>
 * @since ANNCSR 2.0
 */
public class BorderStyleSelectPane extends JBasePane {

	private IconPaintToolBarButton btnDisplay;
	private AbstractButton btnTrigger;
	private BorderStyleControlWindow popupWin;
	private BorderStyle cellBorderStyle;
	private static final BorderStyle[] borderStyle = {
			BorderStyle.getInstance(),
			BorderStyle.getInstance(0, Color.BLACK, 0, Color.BLACK, 1, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(0, Color.BLACK, 1, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(0, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 1, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(0, Color.BLACK, 0, Color.BLACK, 6, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(0, Color.BLACK, 0, Color.BLACK, 5, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(1, Color.BLACK, 0, Color.BLACK, 1, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(1, Color.BLACK, 0, Color.BLACK, 6, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(1, Color.BLACK, 0, Color.BLACK, 5, Color.BLACK, 0, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(1, Color.BLACK, 1, Color.BLACK, 1, Color.BLACK, 1, Color.BLACK, 1, Color.BLACK, 1,
					Color.BLACK),
			BorderStyle.getInstance(1, Color.BLACK, 1, Color.BLACK, 1, Color.BLACK, 1, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK),
			BorderStyle.getInstance(5, Color.BLACK, 5, Color.BLACK, 5, Color.BLACK, 5, Color.BLACK, 0, Color.BLACK, 0,
					Color.BLACK) };
	private List normalStyleList;
	private static final String[] BorderStyleTooltips = { "", "", "", "", "", "", "", "", "", "", "", "" };

	public BorderStyleSelectPane(int width, int height) {
		this(width, height, null);
	}

	public BorderStyleSelectPane(int width, int height, Icon icon) {
		this.cellBorderStyle = new BorderStyle();

		this.normalStyleList = new ArrayList();
		this.normalStyleList.clear();
		for (int i = 0; i < borderStyle.length; i++) {
			this.normalStyleList.add(borderStyle[i]);
		}

		setLayout(new BorderLayout());
		setOpaque(false);
		setBackground(null);
		this.btnDisplay = new IconPaintToolBarButton(icon);
		this.btnDisplay.setRolloverEnabled(true);
		this.btnDisplay.setPreferredSize(new Dimension(width, height));
		this.btnTrigger = new ToolBarButton(ImageUtils.readIcon("/resources/images/gui/popup.gif"));
		this.btnTrigger.setRolloverEnabled(true);
		this.btnTrigger.setPreferredSize(new Dimension(10, height));
		this.btnDisplay.setBackground(Color.WHITE);
		this.btnTrigger.setBackground(Color.WHITE);
		this.btnDisplay.addMouseListener(new DoubleToolBarButtonBorderMouseListener(this.btnTrigger));
		this.btnTrigger.addMouseListener(new DoubleToolBarButtonBorderMouseListener(this.btnDisplay));
		this.btnDisplay.addActionListener(new ActionListener() {

			public void actionPerformed(ActionEvent e) {
				fireStyleStateChanged();
			}
		});
		this.btnTrigger.addMouseListener(new PopupMouseListener());
		add(this.btnDisplay, BorderLayout.CENTER);
		add(this.btnTrigger, BorderLayout.EAST);
		this.btnTrigger.addFocusListener(new FocusListener() {

			public void focusGained(FocusEvent focusEvent) {
			}

			public void focusLost(FocusEvent focusEvent) {
				hidePopupMenu();
			}
		});
	}

	public BorderStyle getCellBorderStyle() {
		return this.cellBorderStyle;
	}

	public void setCellBorderStyle(BorderStyle cellBorderStyle) {
		this.cellBorderStyle = cellBorderStyle;

		if (this.normalStyleList.contains(cellBorderStyle)) {
			this.btnDisplay.setCellBorderStyle(cellBorderStyle);
		}
		fireStyleStateChanged();
	}

	@Override
	public void setEnabled(boolean paramBoolean) {
		super.setEnabled(paramBoolean);
		if (!(paramBoolean)) {
			this.cellBorderStyle = null;
		}
		this.btnDisplay.setEnabled(paramBoolean);
		this.btnTrigger.setEnabled(paramBoolean);
	}

	@Override
	public void setToolTipText(String paramString) {
		super.setToolTipText(paramString);
		this.btnDisplay.setToolTipText(paramString);
		this.btnTrigger.setToolTipText(paramString);
	}

	private void showPopupMenu() {
		if ((this.popupWin != null) && (this.popupWin.isVisible())) {
			hidePopupMenu();
			return;
		}
		if (!(isEnabled())) {
			return;
		}
		this.popupWin = getBorderStyleControlWindow();

		GuiUtils.showPopupMenu(this.popupWin, this, 0, getSize().height);
	}

	private void hidePopupMenu() {
		if (this.popupWin != null) {
			this.popupWin.setVisible(false);
		}
		this.popupWin = null;
	}

	public void addStyleChangeListener(ChangeListener listener) {
		this.listenerList.add(ChangeListener.class, listener);
	}

	public void removeColorChangeListener(ChangeListener listener) {
		this.listenerList.remove(ChangeListener.class, listener);
	}

	public void fireStyleStateChanged() {
		ChangeEvent event = new ChangeEvent(this);
		for (ChangeListener listener : this.listenerList.getListeners(ChangeListener.class)) {
			listener.stateChanged(event);
		}
	}

	private BorderStyleControlWindow getBorderStyleControlWindow() {
		if (this.popupWin == null) {
			this.popupWin = new BorderStyleControlWindow();
		}
		return this.popupWin;
	}

	class DoubleToolBarButtonBorderMouseListener extends MouseAdapter {

		private JComponent component;
		private Border enableBorder;

		public DoubleToolBarButtonBorderMouseListener(JComponent component) {
			this.component = component;
			this.enableBorder = BorderFactory.createEtchedBorder();
		}

		@Override
		public void mouseEntered(MouseEvent mouseEvent) {
			this.component.setBorder(this.enableBorder);
		}

		@Override
		public void mouseExited(MouseEvent mouseEvent) {
			this.component.setBorder(null);
		}
	}

	class IconPaintToolBarButton extends ToolBarButton {

		private BorderStyle cellBorderStyle = new BorderStyle();

		public IconPaintToolBarButton(Icon icon) {
			super(icon);
		}

		public BorderStyle getCellBorderStyle() {
			return this.cellBorderStyle;
		}

		public void setCellBorderStyle(BorderStyle cellBorderStyle) {
			this.cellBorderStyle = cellBorderStyle;
			if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[0]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/noboder.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[1]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/bottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[2]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/left.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[3]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/right.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[4]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/doublebottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[5]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/thickbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[6]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[7]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topanddoublebotttom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[8]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topandthickbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[9]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/allborders.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[10]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/outsideborders.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[11]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/thickoutsideborders.png"));
			}
			repaint();
		}
	}

	private class BorderStyleCell extends JLabel {

		private BorderStyle cellBorderStyle;

		public BorderStyleCell() {
			this.cellBorderStyle = new BorderStyle();
			enableEvents(AWTEvent.MOUSE_EVENT_MASK);
			setPreferredSize(new Dimension(0, 20));
			setCursor(new Cursor(Cursor.HAND_CURSOR));
		}

		public BorderStyleCell(BorderStyle cellBorderStyle) {
			this();
			setCellBorderStyle(cellBorderStyle);
			if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[0]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/noboder.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[1]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/bottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[2]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/left.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[3]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/right.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[4]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/doublebottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[5]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/thickbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[6]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[7]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topanddoublebotttom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[8]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/topandthickbottom.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[9]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/allborders.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[10]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/outsideborders.png"));
			} else if (this.cellBorderStyle == BorderStyleSelectPane.borderStyle[11]) {
				setIcon(ImageUtils.readIcon("/resources/images/format/thickoutsideborders.png"));
			}
		}

		public void setCellBorderStyle(BorderStyle cellBorderStyle) {
			this.cellBorderStyle = cellBorderStyle;
			repaint();
		}

		public BorderStyle getCellBorderStyle() {
			return this.cellBorderStyle;
		}

		@Override
		public void processMouseEvent(MouseEvent mouseEvent) {
			if ((mouseEvent == null) || (mouseEvent.getID() == MouseEvent.MOUSE_RELEASED)) {
				BorderStyleSelectPane.this.setCellBorderStyle(this.cellBorderStyle);
				hidePopupMenu();
			}
			if (mouseEvent == null) {
				return;
			}
			super.processMouseEvent(mouseEvent);
		}

		@Override
		public Dimension getMinimumSize() {
			return getPreferredSize();
		}
	}

	class NormalBorderPane extends JPanel {

		public NormalBorderPane() {
			setLayout(new BorderLayout());
			setBorder(BorderFactory.createEmptyBorder(2, 4, 2, 4));

			JPanel pane = new JPanel();
			add(pane, BorderLayout.CENTER);
			pane.setLayout(new BoxLayout(pane, BoxLayout.Y_AXIS));
			JPanel paneContent = new JPanel();
			pane.add(paneContent);
			paneContent.setLayout(new GridLayout(3, 4, 2, 2));
			for (int i = 0; i < BorderStyleSelectPane.borderStyle.length; i++) {
				final BorderStyleCell cell = new BorderStyleCell(BorderStyleSelectPane.borderStyle[i]);
				cell.addMouseListener(new MouseAdapter() {

					@Override
					public void mouseEntered(MouseEvent paramMouseEvent) {
						cell.setBorder(BorderFactory.createEtchedBorder());
					}

					@Override
					public void mouseExited(MouseEvent paramMouseEvent) {
						cell.setBorder(null);
					}
				});
				cell.setToolTipText(BorderStyleSelectPane.BorderStyleTooltips[i]);
				paneContent.add(cell);
			}
			pane.add(Box.createVerticalStrut(5));
			pane.add(new JSeparator());
			pane.add(Box.createVerticalStrut(5));
			CustomButton buttonCustom = new CustomButton("自定义");
			pane.add(buttonCustom, BorderLayout.SOUTH);
			buttonCustom.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}

		class CustomButton extends JButton {

			public CustomButton(String name) {
				super(name);
				enableEvents(AWTEvent.MOUSE_EVENT_MASK);
			}

			@Override
			public void processMouseEvent(MouseEvent mouseEvent) {
				switch (mouseEvent.getID()) {
				case MouseEvent.MOUSE_PRESSED:
					BorderStyleSelectPane.this.hidePopupMenu();
					JWorksheet workSheet = ReportContext.INSTANCE.getCurrentWorkSheet();
					BorderStylePane borderPane = new BorderStylePane();
					//
					// BasicDialog basicDialog = borderPane.
					// (SwingUtilities.getWindowAncestor(workSheet), "边框", 450,
					// 350);
					// borderPane.populate(workSheet);
					// basicDialog.setVisible(true);
					// int i = basicDialog.getReturnValue();
					// if (i == 0) {
					// BorderStyle cellBorderStyle = borderPane.update();
					// BorderStyleDialog.this.setCellBorderStyle(cellBorderStyle);
					// }
					return;
				}
				super.processMouseEvent(mouseEvent);
			}
		}
	}

	class BorderStyleControlWindow extends JPopupMenu {

		public BorderStyleControlWindow() {
			initComponents();
		}

		public void initComponents() {
			setLightWeightPopupEnabled(JPopupMenu.getDefaultLightWeightPopupEnabled());
			setLayout(new BorderLayout());
			setBorderPainted(true);
			setBorder(UIManager.getBorder("PopupMenu.border"));
			setOpaque(false);
			setDoubleBuffered(true);
			setFocusable(false);
			add(new NormalBorderPane(), BorderLayout.CENTER);
			pack();
		}
	}

	class PopupMouseListener extends MouseAdapter {

		@Override
		public void mousePressed(MouseEvent paramMouseEvent) {
			BorderStyleSelectPane.this.showPopupMenu();
		}
	}
}